<template>
  <van-datetime-picker v-model="currentDate" type="date" title="出生日期" :min-date="minDate" :max-date="maxDate"
    @cancel="$emit('close')" @confirm="onConfirm" @change="onChange" />
</template>

<script>
// 导入接口
import { changeUserProfile } from '@/api/user.js'
import dayJs from 'dayjs'
export default {
  props: {
    value: {
      type: [String, Number],
      require: true
    }
  },
  name: 'Update-birthday',
  data () {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
      // birthday: this.value
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        duration: 0, // 持续展示toast
        message: '修改中',
        forbidclick: 'true' // 禁止点击背景
      })
      try {
        await changeUserProfile({
          birthday: dayJs(this.currentDate).format('YYYY-MM-DD')
        })
        this.$emit('input', dayJs(this.currentDate).format('YYYY-MM-DD'))
        this.$emit('close')
        this.$toast('更新成功')
      } catch (err) {
        console.log(err)
        this.$toast('更新失败，请重试')
      }
    },
    onChange (picker, value, index) {
      // console.log(picker, value, index)
      this.gender = index
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style lang='less' scoped>
</style>
